// copyright:no copyright,use it if you like
// license boilerplate:BSD 3 clause
// filename:Calculator.h
// author:Tempest(https://gitee.com/Tempest56890)
// date:2021-11-18
// brief:implement the function of calculator

#include <iostream>
#include <string>
#include <vector>
using namespace std;
const double INF=0x3f3f3f3f+1.0;
class ExpTree {
public:
    void operator_insert(const char op,const int pr1,const int pr2) {
        operator_insert(op,pr1,pr2,root);
    };
    void calculate(vector<double> & vec_number) {
        calculate(root,vec_number);
    };
    double result() {
        return root->number;
    };
    
private:
    int pos=0;
    struct ExpNode {
        int number_of_number=0;
        int old_n_of_n=0;
        char func='c';
        char ope='#';
        double number=-INF;
        int priority1=0;//一级优先度，用于判断运算符在几层括号内
        int priority2=0;//二级优先度，+，-，*，/分别为1，2，3，4
        ExpNode *left;
        ExpNode *right;
        ExpNode(char fun,const char & oprt,const double & num,
                int p1,int p2,
                ExpNode*lt,ExpNode*rt)
        : func(fun),ope(oprt),number(num),
        priority1(p1),priority2(p2),
        left(lt),right(rt) {}
    };
    ExpNode *root=new ExpNode('c','+',-INF,0,1,nullptr,nullptr);
    void operator_insert(const char op,const int pr1,const int pr2,ExpNode * & t ) {
        if (t==nullptr)
            t=new ExpNode('c',op,-INF,pr1,pr2,nullptr,nullptr);
        else if (t->right!=nullptr && (pr1<t->right->priority1 ||
                 (pr1==t->right->priority1 && pr2<=t->right->priority2))) {
            ExpNode *new_Node=new ExpNode('c',op,-INF,pr1,pr2,nullptr,nullptr);
            new_Node->left=t->right;
            t->right=new_Node;
        }
        else
            operator_insert(op,pr1,pr2,t->right);
    };
    double getnum(ExpNode * & t,vector<double> & vec_number) {
        if (t==nullptr)
            return vec_number[pos++];
        else
            return t->number;
    }
    void calculate(ExpNode * & t,vector<double> & vec_number) {
        if (t->left!=nullptr && t->right!=nullptr) {
            if (t->left->number==-INF) calculate(t->left,vec_number);
            if (t->right->number==-INF) calculate(t->right,vec_number);
            if (t->ope=='+')
                t->number=getnum(t->left,vec_number)+getnum(t->right,vec_number);
            else if (t->ope=='-') t->number=getnum(t->left,vec_number)-getnum(t->right,vec_number);
            else if (t->ope=='*')
                t->number=getnum(t->left,vec_number)*getnum(t->right,vec_number);
            else
                t->number=getnum(t->left,vec_number)/getnum(t->right,vec_number);
        }
        else if (t->left!=nullptr) {
            if (t->left->number==-INF) calculate(t->left,vec_number);
            if (t->ope=='+')
                t->number=getnum(t->left,vec_number)+getnum(t->right,vec_number);
            else if (t->ope=='-') t->number=getnum(t->left,vec_number)-getnum(t->right,vec_number);
            else if (t->ope=='*')
                t->number=getnum(t->left,vec_number)*getnum(t->right,vec_number);
            else
                t->number=getnum(t->left,vec_number)/getnum(t->right,vec_number);
        }
        else if (t->right!=nullptr) {
            if (t->right->number==-INF) calculate(t->right,vec_number);
            if (t->ope=='+')
                t->number=getnum(t->left,vec_number)+getnum(t->right,vec_number);
            else if (t->ope=='-') t->number=getnum(t->left,vec_number)-getnum(t->right,vec_number);
            else if (t->ope=='*')
                t->number=getnum(t->left,vec_number)*getnum(t->right,vec_number);
            else
                t->number=getnum(t->left,vec_number)/getnum(t->right,vec_number);
        }
        else {
            if (t->ope=='+')
                t->number=getnum(t->left,vec_number)+getnum(t->right,vec_number);
            else if (t->ope=='-') t->number=getnum(t->left,vec_number)-getnum(t->right,vec_number);
            else if (t->ope=='*')
                t->number=getnum(t->left,vec_number)*getnum(t->right,vec_number);
            else
                t->number=getnum(t->left,vec_number)/getnum(t->right,vec_number);
        }
    };
    void makeEmpty(ExpNode * & t) {
        if (t!=nullptr) {
            makeEmpty(t->left);
            makeEmpty(t->right);
            delete t;
        }
        t=nullptr;
    };
};

bool judge_if_legal(const std::vector<char> & vec_of_oprt,
                    const std::vector<double> & vec_of_num) {
    char element_of_oprt;
    int bracket=0,count_of_oprt=0,b_b=0;
    for (int i=0;i<vec_of_oprt.size();i++) {
        element_of_oprt=vec_of_oprt[i];
        if (element_of_oprt=='(') {
            bracket++;
        }
        else if (element_of_oprt==')') {
            bracket--;
        }
        if (bracket<0) return false;
        if (element_of_oprt=='+' || element_of_oprt=='-') {
            b_b++;
            count_of_oprt++;
        }
        if (element_of_oprt=='/' || element_of_oprt=='*') {
            b_b++;
            if (element_of_oprt=='/' && vec_of_num[b_b]==0.0) return false;
            count_of_oprt++;
        }
    }
    if (vec_of_num.size()!=count_of_oprt+2) return false;
    return true;
};

int main() {
    string vec;
    freopen("input.txt","r",stdin);
    
    while (getline(cin,vec)) {
    vector<char> vec0;//txt读入的初始式子
    vector<char> vec_operator;//存储精修过的所有操作符，包括括号，'.'已被处理
    vector<double> vec_number;//存储负号必前置数字（至少0.0）的所有数
    vector<int> vec_priority1,vec_priority2;//分别存储两种优先度,括号默认为0
    int bracket_depth=0;//分别统计括号深度,负号前是否有相邻数字
    int dot_depth=10;//判断数字在小数点后几位
    bool num_count=true,num_just_count=false,dot=false;//分别判断数字统计是否进行,当前字符前是否在统计数字
                                                     //当前数字统计是否已经出现小数点
    double temporary=0;
    char ch;
    double sum=0.0;
    
        for (int i=0;i<vec.size();i++) {
            vec0.push_back(vec[i]);
        }
 
    vec_number.push_back(0.0);
    for (int i=0;i<vec0.size();++i) {
        ch=vec0[i];
        if (ch=='(') {
            num_count=false;
            bracket_depth++;
            vec_operator.push_back(ch);
            vec_priority1.push_back(-1);
            vec_priority2.push_back(0);
        }
        else if (ch==')') {
            num_count=false;
            bracket_depth--;
            vec_operator.push_back(ch);
            vec_priority1.push_back(-1);
            vec_priority2.push_back(0);
        }
        else if (ch=='+' || ch=='-' || ch=='/' || ch=='*') {
            num_count=false;
            if (ch=='-' && (i==0 || vec0[i-1]=='(' || vec0[i-1]=='+')) {
                vec_number.push_back(0.0);
            }
            vec_operator.push_back(ch);
            vec_priority1.push_back(bracket_depth);
            if (ch=='+') vec_priority2.push_back(1);
            else if (ch=='-') vec_priority2.push_back(2);
            else if (ch=='*') vec_priority2.push_back(3);
            else vec_priority2.push_back(4);
        }
        else if ('0'<=ch && ch<='9') {
            num_count=true;num_just_count=true;
            temporary=ch-'0'+0.0;
            if (!dot) {
                sum=sum*10+temporary;
            }
            else {
                sum+=temporary/dot_depth;
                dot_depth*=10;
            }
        }
        else if (ch=='.') {
            dot=true;num_count=true;num_just_count=true;
            if (i==(vec0.size()-1) ||
                (i!=(vec0.size()-1) && !('0'<=vec0[i+1] && vec0[i+1]<='9'))) {
                num_count=false;
            }
        }
        else {
            continue;
        }
        if ((!num_count && num_just_count) ||
            (i==vec0.size()-1 && (vec0[i]=='.' || ('0'<=vec0[i] && vec0[i]<='9')))) {
            vec_number.push_back(sum);
            sum=0.0;
            num_just_count=false;
            dot_depth=10;
            dot=false;
        }
    }
    ExpTree tree;
    if (!judge_if_legal(vec_operator,vec_number)) {
        cout<<"Illegal Input."<<endl;
    }
    else {
        for (int i=0;i<vec_operator.size();i++)
            if(vec_operator[i]!='(' && vec_operator[i]!=')')
                tree.operator_insert(vec_operator[i],vec_priority1[i],vec_priority2[i]);
        tree.calculate(vec_number);
        for (int i=0;i<vec0.size();i++) cout<<vec0[i];
        cout<<" : "<<tree.result()<<endl;
    }
    }
    fclose(stdin);
    return 0;
}
